import Vue from 'vue'
import VueRouter from 'vue-router'

import AdminHome from '@/views/server/AdminHome.vue'
import AdminMain from '@/views/server/AdminMain.vue'
import AdminLogin from '@/views/server/AdminLogin.vue'
import ActiveAdd from '@/views/server/ActiveAdd.vue'
import AdminActive from "@/views/server/AdminActiveExamine.vue"
import AdminActiveDetail from "@/views/server/AdminActiveDetail.vue"
import AdminUserManager from "@/views/server/AdminUserManager.vue"
import AdminAuthentication from '@/views/server/AdminAuthentication.vue'


import error from '@/views/error/404.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/admin',
    name: 'AdminHome',
    component: AdminHome,
    redirect: '/main',
    children: [
      {
        path: '/main',
        name: 'AdminMain',
        component: AdminMain
      },
      {
        path: '/insert',
        name: 'ActiveAdd',
        component: ActiveAdd
      },
      {
        path: '/examine',
        name: 'AdminActive',
        component: AdminActive
      },
      {
        path: '/detail',
        name: 'AdminActiveDetail',
        component: AdminActiveDetail
      },
      {
        path: '/user-manager',
        name: 'AdminUserManager',
        component: AdminUserManager
      },
      {
        path: '/authentication',
        name: 'AdminAuthentication',
        component: AdminAuthentication
      },
    ]
  },
  {
    path: '/alogin',
    name: 'ALogin',
    component: AdminLogin
  },
  {
    path: '/error',
    name: 'error',
    component: error
  },
  {
    path: '/ActiveLogin',
    name: 'ActiveLogin',
    component: () => import('../views/cline/ActiveLogin.vue')
  },
  {
    path: '/',
    name: 'ActiveHome',
    redirect: '/ActiveShow',
    component: () => import('../views/cline/ActiveHome.vue'),
    children: [
      {
        path: '/ActiveShow',
        name: 'ActiveShow',
        component: () => import('../views/cline/ActiveShow.vue')
      },
      {
        path: '/MyActive',
        name: 'MyActive',
        component: () => import('../views/cline/MyActive.vue')
      },
      {
        path:'/MyScore',
        name:'MyScore',
        component:()=>import('../views/cline/MyScore.vue')
      },
      {
        path:'/UserCenter',
        name:'UserCenter',
        component:()=>import('../views/cline/UserCenter.vue')
      }
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, form, next) => {
  var token = localStorage.getItem('token')
  var user=localStorage.getItem('userInfo');
  if(user===undefined || user===null)
  {
    user=JSON.parse(user)
  }
  if (!router.getMatchedComponents(to).length > 0) {
    next({ path: '/error' })
    return
  }
  if (to.name === 'ALogin' || to.name==='ActiveLogin') {
    next()
    return;
  }
  if (token === null || user === null) {
    router.push('/ActiveLogin')
    return;
  }

  next()
})

export default router
